package com.airnews.ticket.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.view.KeyEvent;

import com.airnews.ticket.R;




/**
 * 
 *
 */
public class NetUtils {

	public static Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
	
	
	public static boolean isNetworkAvailable(Context context) {  
	    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
	    if (connectivity == null) {     
	      return false; 
	    } else {   
	        NetworkInfo info = connectivity.getActiveNetworkInfo();     
	        if (info != null) {         
	        	if (info.getState() == NetworkInfo.State.CONNECTED) {               
                    return true;
                }
	        }  
	    }    
	    return false; 
	}	
	
	/**
	 * 检测网络是否存在
	 */
	public static boolean httpTest(final Activity mActivity) {
		if (!isNetworkAvailable(mActivity)) {
	        AlertDialog alert = new AlertDialog.Builder(mActivity)
	        .setCancelable(false)
	        .setOnKeyListener(onKeyListener)
            //.setIcon(R.drawable.alert_dialog_icon)
            .setTitle(R.string.alert_dialog_error)
            .setMessage(R.string.alert_dialog_net_fail)
            .setPositiveButton(R.string.alert_dialog_yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {   
                	mActivity.finish();;   
                }
            })
            .create();
			alert.show();
			
			return false;
		}
		return true;
	}
	
	
	/**
	 * 屏蔽键盘事件
	 */
	private static OnKeyListener onKeyListener = new OnKeyListener(){
		@Override
		public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
			return true;
		}
	};

}
